﻿using System;
using System.Collections.Generic;
using System.Xml;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.MapEvents;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Party.PartyComponents;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.ObjectSystem;
using TaleWorlds.SaveSystem;

namespace TaleWorlds.CampaignSystem.Settlements
{
	// Token: 0x0200035F RID: 863
	public class Village : SettlementComponent
	{
		// Token: 0x0600324E RID: 12878 RVA: 0x000D272B File Offset: 0x000D092B
		internal static void AutoGeneratedStaticCollectObjectsVillage(object o, List<object> collectedObjects)
		{
			((Village)o).AutoGeneratedInstanceCollectObjects(collectedObjects);
		}

		// Token: 0x0600324F RID: 12879 RVA: 0x000D2739 File Offset: 0x000D0939
		protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
		{
			base.AutoGeneratedInstanceCollectObjects(collectedObjects);
			collectedObjects.Add(this._bound);
			collectedObjects.Add(this._marketData);
		}

		// Token: 0x06003250 RID: 12880 RVA: 0x000D275A File Offset: 0x000D095A
		internal static object AutoGeneratedGetMemberValueLastDemandSatisfiedTime(object o)
		{
			return ((Village)o).LastDemandSatisfiedTime;
		}

		// Token: 0x06003251 RID: 12881 RVA: 0x000D276C File Offset: 0x000D096C
		internal static object AutoGeneratedGetMemberValueHearth(object o)
		{
			return ((Village)o).Hearth;
		}

		// Token: 0x06003252 RID: 12882 RVA: 0x000D277E File Offset: 0x000D097E
		internal static object AutoGeneratedGetMemberValueTradeTaxAccumulated(object o)
		{
			return ((Village)o).TradeTaxAccumulated;
		}

		// Token: 0x06003253 RID: 12883 RVA: 0x000D2790 File Offset: 0x000D0990
		internal static object AutoGeneratedGetMemberValue_villageState(object o)
		{
			return ((Village)o)._villageState;
		}

		// Token: 0x06003254 RID: 12884 RVA: 0x000D27A2 File Offset: 0x000D09A2
		internal static object AutoGeneratedGetMemberValue_bound(object o)
		{
			return ((Village)o)._bound;
		}

		// Token: 0x06003255 RID: 12885 RVA: 0x000D27AF File Offset: 0x000D09AF
		internal static object AutoGeneratedGetMemberValue_marketData(object o)
		{
			return ((Village)o)._marketData;
		}

		// Token: 0x17000C36 RID: 3126
		// (get) Token: 0x06003256 RID: 12886 RVA: 0x000D27BC File Offset: 0x000D09BC
		public static MBReadOnlyList<Village> All
		{
			get
			{
				return Campaign.Current.AllVillages;
			}
		}

		// Token: 0x06003257 RID: 12887 RVA: 0x000D27C8 File Offset: 0x000D09C8
		public IEnumerable<PartyBase> GetDefenderParties(MapEvent.BattleTypes battleType)
		{
			yield return base.Settlement.Party;
			foreach (MobileParty mobileParty in base.Settlement.Parties)
			{
				if (mobileParty.MapFaction == base.Settlement.MapFaction && ((!mobileParty.IsMilitia && !mobileParty.IsVillager) || battleType == MapEvent.BattleTypes.Raid || battleType == MapEvent.BattleTypes.IsForcingSupplies || battleType == MapEvent.BattleTypes.IsForcingVolunteers) && !mobileParty.IsCaravan)
				{
					yield return mobileParty.Party;
				}
			}
			List<MobileParty>.Enumerator enumerator = default(List<MobileParty>.Enumerator);
			yield break;
			yield break;
		}

		// Token: 0x06003258 RID: 12888 RVA: 0x000D27E0 File Offset: 0x000D09E0
		public PartyBase GetNextDefenderParty(ref int partyIndex, MapEvent.BattleTypes battleType)
		{
			partyIndex++;
			if (partyIndex == 0)
			{
				return base.Settlement.Party;
			}
			for (int i = partyIndex - 1; i < base.Settlement.Parties.Count; i++)
			{
				MobileParty mobileParty = base.Settlement.Parties[i];
				if (mobileParty.MapFaction == base.Settlement.MapFaction && !mobileParty.IsCaravan)
				{
					partyIndex = i + 1;
					return mobileParty.Party;
				}
			}
			return null;
		}

		// Token: 0x17000C37 RID: 3127
		// (get) Token: 0x06003259 RID: 12889 RVA: 0x000D285B File Offset: 0x000D0A5B
		// (set) Token: 0x0600325A RID: 12890 RVA: 0x000D2864 File Offset: 0x000D0A64
		public Village.VillageStates VillageState
		{
			get
			{
				return this._villageState;
			}
			set
			{
				if (this._villageState != value)
				{
					this._villageState = value;
					switch (this._villageState)
					{
					case Village.VillageStates.Normal:
						CampaignEventDispatcher.Instance.OnVillageBecomeNormal(this);
						return;
					case Village.VillageStates.BeingRaided:
						CampaignEventDispatcher.Instance.OnVillageBeingRaided(this);
						return;
					case Village.VillageStates.ForcedForVolunteers:
					case Village.VillageStates.ForcedForSupplies:
						break;
					case Village.VillageStates.Looted:
						CampaignEventDispatcher.Instance.OnVillageLooted(this);
						break;
					default:
						return;
					}
				}
			}
		}

		// Token: 0x17000C38 RID: 3128
		// (get) Token: 0x0600325B RID: 12891 RVA: 0x000D28C6 File Offset: 0x000D0AC6
		public bool IsDeserted
		{
			get
			{
				return this._villageState == Village.VillageStates.Looted;
			}
		}

		// Token: 0x17000C39 RID: 3129
		// (get) Token: 0x0600325C RID: 12892 RVA: 0x000D28D1 File Offset: 0x000D0AD1
		// (set) Token: 0x0600325D RID: 12893 RVA: 0x000D28D9 File Offset: 0x000D0AD9
		[SaveableProperty(105)]
		public float LastDemandSatisfiedTime { get; private set; }

		// Token: 0x17000C3A RID: 3130
		// (get) Token: 0x0600325E RID: 12894 RVA: 0x000D28E2 File Offset: 0x000D0AE2
		// (set) Token: 0x0600325F RID: 12895 RVA: 0x000D28EA File Offset: 0x000D0AEA
		public Settlement Bound
		{
			get
			{
				return this._bound;
			}
			private set
			{
				if (this._bound != value)
				{
					Settlement bound = this._bound;
					if (bound != null)
					{
						bound.RemoveBoundVillageInternal(this);
					}
					this._bound = value;
					Settlement bound2 = this._bound;
					if (bound2 == null)
					{
						return;
					}
					bound2.AddBoundVillageInternal(this);
				}
			}
		}

		// Token: 0x17000C3B RID: 3131
		// (get) Token: 0x06003260 RID: 12896 RVA: 0x000D291F File Offset: 0x000D0B1F
		// (set) Token: 0x06003261 RID: 12897 RVA: 0x000D293C File Offset: 0x000D0B3C
		public Settlement TradeBound
		{
			get
			{
				if (!this._bound.IsTown)
				{
					return this._tradeBound;
				}
				return this._bound;
			}
			internal set
			{
				if (this._tradeBound != value && !this._bound.IsTown)
				{
					Settlement tradeBound = this._tradeBound;
					if (tradeBound != null)
					{
						tradeBound.Town.RemoveTradeBoundVillageInternal(this);
					}
					this._tradeBound = value;
					Settlement tradeBound2 = this._tradeBound;
					if (tradeBound2 == null)
					{
						return;
					}
					tradeBound2.Town.SetTradeBoundVillageInternal(this);
				}
			}
		}

		// Token: 0x17000C3C RID: 3132
		// (get) Token: 0x06003262 RID: 12898 RVA: 0x000D2993 File Offset: 0x000D0B93
		public VillageMarketData MarketData
		{
			get
			{
				return this._marketData;
			}
		}

		// Token: 0x17000C3D RID: 3133
		// (get) Token: 0x06003263 RID: 12899 RVA: 0x000D299B File Offset: 0x000D0B9B
		// (set) Token: 0x06003264 RID: 12900 RVA: 0x000D29A3 File Offset: 0x000D0BA3
		[SaveableProperty(108)]
		public float Hearth { get; set; }

		// Token: 0x17000C3E RID: 3134
		// (get) Token: 0x06003265 RID: 12901 RVA: 0x000D29AC File Offset: 0x000D0BAC
		// (set) Token: 0x06003266 RID: 12902 RVA: 0x000D29B4 File Offset: 0x000D0BB4
		[SaveableProperty(110)]
		public int TradeTaxAccumulated { get; set; }

		// Token: 0x06003267 RID: 12903 RVA: 0x000D29BD File Offset: 0x000D0BBD
		public Village()
		{
			this.LastDemandSatisfiedTime = -1f;
			this._marketData = new VillageMarketData(this);
		}

		// Token: 0x06003268 RID: 12904 RVA: 0x000D29DC File Offset: 0x000D0BDC
		public void DailyTick()
		{
			int hearthLevel = this.GetHearthLevel();
			this.Hearth += this.HearthChange;
			if (hearthLevel != this.GetHearthLevel())
			{
				base.Settlement.Party.SetLevelMaskIsDirty();
			}
			if (this.Hearth < 10f)
			{
				this.Hearth = 10f;
			}
			base.Owner.Settlement.Militia += this.MilitiaChange;
			if (base.Gold > 1000)
			{
				base.ChangeGold(1000 - base.Gold);
			}
		}

		// Token: 0x06003269 RID: 12905 RVA: 0x000D2A6E File Offset: 0x000D0C6E
		public override void OnInit()
		{
			ChangeVillageStateAction.ApplyBySettingToNormal(base.Settlement);
			base.ChangeGold(1000);
		}

		// Token: 0x0600326A RID: 12906 RVA: 0x000D2A88 File Offset: 0x000D0C88
		public int GetWerehouseCapacity()
		{
			float num = Campaign.Current.Models.VillageProductionCalculatorModel.CalculateDailyFoodProductionAmount(this);
			foreach (ValueTuple<ItemObject, float> valueTuple in this.VillageType.Productions)
			{
				float num2 = Campaign.Current.Models.VillageProductionCalculatorModel.CalculateDailyProductionAmount(this, valueTuple.Item1);
				num += num2;
			}
			return MathF.Ceiling(MathF.Max(1f, num) * 5f);
		}

		// Token: 0x0600326B RID: 12907 RVA: 0x000D2B28 File Offset: 0x000D0D28
		public override int GetItemPrice(ItemObject item, MobileParty tradingParty = null, bool isSelling = false)
		{
			if (this.TradeBound == null)
			{
				return 1;
			}
			return this.TradeBound.Town.MarketData.GetPrice(item, tradingParty, isSelling, null);
		}

		// Token: 0x0600326C RID: 12908 RVA: 0x000D2B4D File Offset: 0x000D0D4D
		public override int GetItemPrice(EquipmentElement itemRosterElement, MobileParty tradingParty = null, bool isSelling = false)
		{
			if (this.TradeBound == null)
			{
				return 1;
			}
			return this.TradeBound.Town.MarketData.GetPrice(itemRosterElement, tradingParty, isSelling, null);
		}

		// Token: 0x0600326D RID: 12909 RVA: 0x000D2B72 File Offset: 0x000D0D72
		public override string ToString()
		{
			return base.Name.ToString();
		}

		// Token: 0x0600326E RID: 12910 RVA: 0x000D2B80 File Offset: 0x000D0D80
		public override void Deserialize(MBObjectManager objectManager, XmlNode node)
		{
			bool isInitialized = base.IsInitialized;
			base.Deserialize(objectManager, node);
			base.BackgroundCropPosition = float.Parse(node.Attributes["background_crop_position"].Value);
			base.BackgroundMeshName = node.Attributes["background_mesh"].Value;
			base.CastleBackgroundMeshName = node.Attributes["castle_background_mesh"].Value;
			base.WaitMeshName = node.Attributes["wait_mesh"].Value;
			if (!isInitialized)
			{
				this.Hearth = (float)int.Parse(node.Attributes["hearth"].Value);
			}
			this.VillageType = (VillageType)objectManager.ReadObjectReferenceFromXml("village_type", typeof(VillageType), node);
			if (!isInitialized)
			{
				this.Bound = (Settlement)objectManager.ReadObjectReferenceFromXml("bound", typeof(Settlement), node);
				if (this.Bound.IsTown)
				{
					this.Bound.Town.SetTradeBoundVillageInternal(this);
				}
			}
		}

		// Token: 0x0600326F RID: 12911 RVA: 0x000D2C94 File Offset: 0x000D0E94
		public bool IsProducing(ItemObject item)
		{
			using (List<ValueTuple<ItemObject, float>>.Enumerator enumerator = this.VillageType.Productions.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Item1 == item)
					{
						return true;
					}
				}
			}
			return false;
		}

		// Token: 0x17000C3F RID: 3135
		// (get) Token: 0x06003270 RID: 12912 RVA: 0x000D2CF4 File Offset: 0x000D0EF4
		public float HearthChange
		{
			get
			{
				return Campaign.Current.Models.SettlementProsperityModel.CalculateHearthChange(this, false).ResultNumber;
			}
		}

		// Token: 0x17000C40 RID: 3136
		// (get) Token: 0x06003271 RID: 12913 RVA: 0x000D2D1F File Offset: 0x000D0F1F
		public float Militia
		{
			get
			{
				return base.Owner.Settlement.Militia;
			}
		}

		// Token: 0x17000C41 RID: 3137
		// (get) Token: 0x06003272 RID: 12914 RVA: 0x000D2D34 File Offset: 0x000D0F34
		public float MilitiaChange
		{
			get
			{
				return Campaign.Current.Models.SettlementMilitiaModel.CalculateMilitiaChange(base.Owner.Settlement, false).ResultNumber;
			}
		}

		// Token: 0x17000C42 RID: 3138
		// (get) Token: 0x06003273 RID: 12915 RVA: 0x000D2D69 File Offset: 0x000D0F69
		public ExplainedNumber MilitiaChangeExplanation
		{
			get
			{
				return Campaign.Current.Models.SettlementMilitiaModel.CalculateMilitiaChange(base.Owner.Settlement, true);
			}
		}

		// Token: 0x17000C43 RID: 3139
		// (get) Token: 0x06003274 RID: 12916 RVA: 0x000D2D8B File Offset: 0x000D0F8B
		public ExplainedNumber HearthChangeExplanation
		{
			get
			{
				return Campaign.Current.Models.SettlementProsperityModel.CalculateHearthChange(this, true);
			}
		}

		// Token: 0x06003275 RID: 12917 RVA: 0x000D2DA3 File Offset: 0x000D0FA3
		public int GetHearthLevel()
		{
			if (this.Hearth >= 600f)
			{
				return 2;
			}
			if (this.Hearth >= 200f)
			{
				return 1;
			}
			return 0;
		}

		// Token: 0x06003276 RID: 12918 RVA: 0x000D2DC4 File Offset: 0x000D0FC4
		public override SettlementComponent.ProsperityLevel GetProsperityLevel()
		{
			if (this.GetHearthLevel() >= 2)
			{
				return SettlementComponent.ProsperityLevel.High;
			}
			if (this.GetHearthLevel() >= 1)
			{
				return SettlementComponent.ProsperityLevel.Mid;
			}
			return SettlementComponent.ProsperityLevel.Low;
		}

		// Token: 0x06003277 RID: 12919 RVA: 0x000D2DDD File Offset: 0x000D0FDD
		protected override void OnInventoryUpdated(ItemRosterElement item, int count)
		{
		}

		// Token: 0x0400103E RID: 4158
		public const int MidHearthThreshold = 600;

		// Token: 0x0400103F RID: 4159
		public const int LowHearthThreshold = 200;

		// Token: 0x04001040 RID: 4160
		private const int InitialVillageGold = 1000;

		// Token: 0x04001041 RID: 4161
		public const int NumberOfDaysToFillVillageStocks = 5;

		// Token: 0x04001042 RID: 4162
		[CachedData]
		public VillagerPartyComponent VillagerPartyComponent;

		// Token: 0x04001043 RID: 4163
		[SaveableField(104)]
		private Village.VillageStates _villageState;

		// Token: 0x04001045 RID: 4165
		[SaveableField(106)]
		private Settlement _bound;

		// Token: 0x04001046 RID: 4166
		public VillageType VillageType;

		// Token: 0x04001047 RID: 4167
		private Settlement _tradeBound;

		// Token: 0x04001048 RID: 4168
		[SaveableField(107)]
		private VillageMarketData _marketData;

		// Token: 0x020006A5 RID: 1701
		public enum VillageStates
		{
			// Token: 0x04001BA9 RID: 7081
			Normal,
			// Token: 0x04001BAA RID: 7082
			BeingRaided,
			// Token: 0x04001BAB RID: 7083
			ForcedForVolunteers,
			// Token: 0x04001BAC RID: 7084
			ForcedForSupplies,
			// Token: 0x04001BAD RID: 7085
			Looted
		}
	}
}
